package com.liugh.controller.zhongche;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.standard.StandardCategory;
import com.liugh.entity.standard.StandardItem;
import com.liugh.service.zhongche.IStandardCategoryService;
import com.liugh.service.zhongche.IStandardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Fatao on 2018/11/12 .
 */

@RestController
@RequestMapping("/standard_category")
@Api(description = "标准分类")
public class StandardCategoryController extends BaseRestController {


    @Autowired
    IStandardCategoryService standardCategoryService;

    @Autowired
    IStandardService standardService;


    /**
     * 添加一个分类 .
     * @param standardCategory .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("添加分类")
    @PostMapping("/add")
    public ResponseModel<StandardCategory> addCategory(@RequestBody StandardCategory standardCategory) throws BusinessException {
        standardCategory = standardCategoryService.addStandardCategory(standardCategory);
        if (standardCategory == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        } else {
            return fillSuccessData(standardCategory);
        }
    }




    /**
     * 更新一个分类 .
     * @param standardCategory .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("更新分类")
    @PutMapping("/update")
    public ResponseModel<StandardCategory> updateCategory(@RequestBody StandardCategory standardCategory) throws BusinessException {
        StandardCategory standardCategory1 = standardCategoryService.updateStandcategoryById(standardCategory);
        if (standardCategory1 == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        } else {
            return fillSuccessData(standardCategory1);
        }
    }


    /**
     * 删除分类 .
     * @param id .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("删除分类")
    @DeleteMapping("/delete")
    public ResponseModel deleteCategory(@RequestParam("id")int id) throws BusinessException {
        if (standardService.selectList(new EntityWrapper<StandardItem>().eq("category", id)).isEmpty()) {
            if (standardCategoryService.deleteCategoryById(id) > 0) {
                return fillSuccessData(PublicResultConstant.SUCCEED);
            } else {
                throw new BusinessException(PublicResultConstant.ERROR);
            }
        } else {
            return fillFailData(PublicResultConstant.ONLY_CAN_DELETE_EMPTY_CATEGORY);
        }

    }


    /**
     * 根据id获取分类 .
     * @param id .
     * @return .
     */
    @ApiOperation("根据id获取分类")
    @GetMapping("/get")
    public ResponseModel<StandardCategory> getCategoryById(@RequestParam("id") int id) {
        return fillSuccessData(standardCategoryService.getCategoryById(id));
    }


    /**
     * 分页获取所有分类 .
     * @param pageNum .
     * @param pageSize .
     * @return .
     */
    @ApiOperation("分页获取所有的分类")
    @GetMapping("/get_all")
    public ResponseModel<Page<StandardCategory>> getAllCategory(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                                @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {

        return fillSuccessDataPage(standardCategoryService.getAllStandCategory(new Page<>(pageNum, pageSize)));
    }


    @ApiOperation("分页获取某个分类下的标准")
    @GetMapping("/item_list")
    public ResponseModel<Page<StandardItem>> getItemPageByCatId(@RequestParam(value = "id",required = true)int id,
                                                                @RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                                @RequestParam(value = "pageSize",required = false,defaultValue = "10")int pageSize) {
        Page<StandardItem> page = new Page<>(pageNum,pageSize);
        return fillSuccessDataPage(standardCategoryService.getItemByCatId(page,id));
    }

}
